package com.zxl.always.on.display.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.zxl.always.on.display.R;

/**
 * Created by Administrator on 2018/2/27.
 */

public class NonePaddingTextView extends TextView {

    private Rect text_bounds = new Rect();

    private boolean mIsVerticalMode = false;

    private final TextPaint mPaint = new TextPaint();

    private final Rect mBounds = new Rect();


    public NonePaddingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setIncludeFontPadding(false);
        TypedArray localTypedArray = getContext().obtainStyledAttributes(attrs, R.styleable.NonePaddingTextView);
        mIsVerticalMode = localTypedArray.getBoolean(R.styleable.NonePaddingTextView_vertical_mode, false);
        localTypedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mIsVerticalMode) {
            getPaint().getTextBounds(getText().toString(), 0, getText().length(), text_bounds);
            setMeasuredDimension(measureHeight(widthMeasureSpec),  measureWidth(heightMeasureSpec));
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            calculateTextParams();
            setMeasuredDimension(mBounds.width() + 1, -mBounds.top + 2);
        }
    }

    private int measureWidth(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = text_bounds.height() + getPaddingTop() + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = text_bounds.width() + getPaddingLeft() + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mIsVerticalMode) {
            canvas.save();
            Path path = new Path();
            int startX = (getWidth() - text_bounds.height() >> 1);
            int startY = (getHeight() - text_bounds.width() >> 1);
            int stopX = (getWidth() - text_bounds.height() >> 1);
            int stopY = (getHeight() + text_bounds.width() >> 1);
            path.moveTo(startX, startY);
            path.lineTo(stopX, stopY);
            this.getPaint().setColor(this.getCurrentTextColor());
            canvas.drawTextOnPath(getText().toString(), path, 0, 0, this.getPaint());
            canvas.restore();
        } else {
            final String text = calculateTextParams();
            final int left = mBounds.left;
            final int bottom = mBounds.bottom;
            mBounds.offset(-mBounds.left, -mBounds.top);
            mPaint.setAntiAlias(true);
            mPaint.setColor(getCurrentTextColor());
            Typeface typeface = getTypeface();
            if (null != typeface) {
                mPaint.setTypeface(typeface);
            }
            canvas.drawText(text, -left, mBounds.bottom - bottom, mPaint);
        }
    }

    private String calculateTextParams() {
        final String text = getText().toString();
        final int textLength = text.length();
        mPaint.setTextSize(getTextSize());
        mPaint.getTextBounds(text, 0, textLength, mBounds);
        if (textLength == 0) {
            mBounds.right = mBounds.left;
        }
        return text;
    }

    @Override
    public TextPaint getPaint() {
        if (mIsVerticalMode) {
            return super.getPaint();
        }
        return mPaint;
    }
}
